<?php

/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/15
 * Time: 15:14
 */
namespace Webview\Api;

use Common\Api\Api;
use Webview\Model\SigninModel;

class SigninApi extends Api{


    const SIGN = "Webview/Signin/sign";

    const GET_INFO = "Webview/Signin/getInfo";


    protected function _init(){
        $this -> model = new SigninModel();
    }

    private function judgeToday($time1,$time2){
        $tmp1 = date("Y-m-d",$time1);
        $tmp2 = date("Y-m-d",$time2);
        return $tmp1 == $tmp2;
    }

    private function judgeYesterday($time1,$time2){
        $tmp1 = getdate($time1);
        $tmp2 = getdate($time2);
        if($tmp1['year'] == $tmp2['year'] && $tmp1['mon'] == $tmp2['mon'] && ($tmp1['mday']-$tmp2['mday']==1)){
            return true;
        }else{
            return false;
        }
    }

    public function sign($uid){

        $map = array(
            'uid' => $uid,
        );

        $score = 5;

        $result = $this -> model -> where($map) -> find();

        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }else{
            if($result == null){
                //如果之前没有签到过
                $entity = array(
                    'uid'=>$uid,
                    'sign_in_time'=>time(),
                    'continues_signin'=>1,
                );
                $result = $this -> model -> create($entity,1);
                if($result === false){
                    $error = $this -> model -> getError();
                    return $this -> apiReturnErr($error);
                }else{
                    $result = $this -> model -> add();
                    if ($result === false) {
                        return $this -> apiReturnErr($this -> model -> getDbError());
                    }
                    return $this -> apiReturnSuc($score);
                }
            }else{
                if($this -> judgeToday(time(),$result['sign_in_time'])){
                    //同一天签到
                    $score = 0;
                    $entity = array(
                        'uid' => $uid,
                        'sign_in_time' => $result['sign_in_time'],
                        'continues_signin' => $result['continues_signin'],
                    );
                }else if($this -> judgeYesterday(time(),$result['sign_in_time'])){
                    //连续签到
                    $score = ( ($score + $result['continues_signin']*5) > 30)? 30 : ($score + $result['continues_signin']*5);

                    $entity = array(
                        'uid' => $uid,
                        'sign_in_time' => time(),
                        'continues_signin' => $result['continues_signin'] + 1,
                    );

                }else{
                    //签到断开
                    $entity = array(
                        'uid' => $uid,
                        'sign_in_time' => time(),
                        'continues_signin' => 1,
                    );
                }
                $result = $this -> model -> create($entity,2);
                if($result === false){
                    $error = $this -> model -> getError();
                    return $this -> apiReturnErr($error);
                }

                $result = $this -> model -> where($map) -> save();
                if ($result === false) {
                    $error = $this -> model -> getDbError();
                    return $this -> apiReturnErr($error);
                }
                return $this -> apiReturnSuc($score);

            }
        }

    }


}